package com.meizi.codegenerate.enums;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.*;

/**
 * @author Administration
 * @date 2022/12/23 - 17:10 - 星期五
 * @project lambda
 * @package com.meizi.codegenerate
 * @by 晨
 * 列名类型 java类型
 */
public enum ColumnDataTypeEnum {

    /**
     * value:对应Mysql数据库类型
     * desc：java类型
     */
    BIGINT("bigint", Long.class),

    BINARY("binary", byte[].class),

    BIT("bit", Boolean.class),

    BLOB("blob", byte[].class),

    CHAR("char", String.class),

    DATE("date", Date.class),

    DATETIME("datetime", Date.class),

    DECIMAL("decimal", BigDecimal.class),

    NUMERIC("numeric", BigDecimal.class),

    DOUBLE("double", Double.class),

    ENUM("enum", Object.class),

    FLOAT("float", Float.class),

    real("real", Float.class),

    INT("int", Integer.class),

    INTEGER("integer", Integer.class),

    JSON("json", String.class),

    LONGBLOB("longblob", byte[].class),

    LONGTEXT("longtext", String.class),

    MEDIUMBLOB("mediumblob", byte[].class),

    MEDIUMINT("mediumint", Integer.class),

    MEDIUMTEXT("mediumtext", String.class),

    MULTIPOINT("multipoint", Integer.class),

    POINT("point", Integer.class),

    SMALLINT("smallint", Integer.class),

    TEXT("text", String.class),

    TIME("time", Time.class),

    TIMESTAMP("timestamp", Date.class),

    TINYBLOB("tinyblob", byte[].class),

    TINYINT("tinyint", Integer.class),

    TINYTEXT("tinytext", String.class),

    VARBINARY("varbinary", byte[].class),

    VARCHAR("varchar", String.class),

    YEAR("year", Date.class),

    ;

    private String value;

    private Class<?> desc;

    /**
     * 枚举缓存
     */
    private static volatile List<Map<String, Object>> cache = null;


    ColumnDataTypeEnum(String value, Class<?> desc) {
        this.value = value;
        this.desc = desc;
    }

    /**
     * 根据 value 找 对应的枚举
     *
     * @param v String
     * @return DoctorRecordStatusAuthStateEnum
     */
    public static ColumnDataTypeEnum from(String v) {
        for (ColumnDataTypeEnum tt : ColumnDataTypeEnum.values()){
            if (tt.getValue().equals(v)){
                return tt;
            }
        }
        throw new IllegalArgumentException("unknown trade type: " + v);
    }

    /**
     * 查找 value 是否存在
     *
     * @param value String 参数
     * @return boolean
     */
    public static boolean containsValue(String value) {
        ColumnDataTypeEnum[] values = ColumnDataTypeEnum.values();
        for (ColumnDataTypeEnum e : values) {
            if (e.getValue().equals(value)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查找 value 是否存在
     *
     * @param value Integer 参数
     * @return boolean
     */
    public static boolean validation(String value) {
        boolean b = containsValue(value);
        return b;
    }

    /**
     * 查找 desc 是否存在
     *
     * @param desc String 参数
     * @return boolean
     */
    public static boolean containsDesc(Class<?> desc) {
        ColumnDataTypeEnum[] values = ColumnDataTypeEnum.values();
        for (ColumnDataTypeEnum e : values) {
            if (e.getDesc().equals(desc)) {
                return true;
            }
        }
        return false;
    }

    /**
     * To Dict
     * @return List<Map<String, Object>>
     */
    public static List<Map<String, Object>> toDict() {
        if(cache != null){
            return cache;
        }

        if(cache == null){
            synchronized(ColumnDataTypeEnum.class) {
                if(cache == null){
                    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                    for (ColumnDataTypeEnum t : ColumnDataTypeEnum.values()) {
                        Map<String, Object> tempMap = new HashMap<String, Object>();
                        tempMap.put("value", t.getValue());
                        tempMap.put("desc", t.getDesc());
                        list.add(tempMap);
                    }
                    cache = list;
                }
            }
        }

        return cache;
    }

    public String getValue() {
        return this.value;
    }

    public Class<?> getDesc() {
        return this.desc;
    }

    public List<Map<String, Object>> getCache() {
        return this.cache;
    }
}
